import pygame
import sys
from random import randint

FPS = 60
speed = 3
t_speed = 60
BLACK = (0, 0, 0)
W, H = 1000, 570
game_score = 0

pygame.init()

clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT, 2000)

balls_data = ({'path': 'ball_bear.png', 'score': 100},
              {'path': 'ball_fox.png', 'score': 150},
              {'path': 'ball_panda.png', 'score': 200})

sc = pygame.display.set_mode((W, H))
icon = pygame.display.set_icon(pygame.image.load('icon/rur.png'))
balls_images = ['ball_bear.png', 'ball_fox.png', 'ball_panda.png']
balls_surf = [pygame.image.load('images/'+data['path']).convert_alpha()
              for data in balls_data]
bg = pygame.image.load('images/back1.jpg').convert()
telega = pygame.image.load('images/telega.png').convert_alpha()
t_rect = telega.get_rect(centerx=W//2, bottom=H-5)
score = pygame.image.load('images/score_fon.png').convert_alpha()
f = pygame.font.SysFont('arial', 30)

balls = pygame.sprite.Group()

'''
clip = VideoFileClip('video/kek.avi').speedx(2).volumex(0.2).subclip(600)
clip.preview(fps=25)
'''


class Ball(pygame.sprite.Sprite):
    def __init__(self, x, speed, surf, score, group):
        pygame.sprite.Sprite.__init__(self)
        self.image = surf
        self.rect = self.image.get_rect(center=(x, 0))
        self.speed = speed
        self.score = score
        self.add(group)

    def update(self, H):
        if self.rect.y < H - self.rect.height:
            self.rect.y += self.speed
        else:
            self.kill()


def createBall(group):
    indx = randint(0, len(balls_surf)-1)
    x = randint(20, W-20)
    speed = randint(1, 4)

    return Ball(x, speed, balls_surf[indx], balls_data[indx]['score'], group)


def collideBalls():
    global game_score
    for ball in balls:
        if t_rect.collidepoint(ball.rect.center):
            game_score += ball.score
            ball.kill()


createBall(balls)
run = True
#some = True

while run:
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
        elif event.type == pygame.USEREVENT:
            createBall(balls)
        elif keys[pygame.K_LEFT]:
            t_rect.x -= t_speed
            if t_rect.x < 0:
                t_rect.x = 0
        elif keys[pygame.K_RIGHT]:
            t_rect.x += t_speed
            if t_rect.x > W-t_rect.width:
                t_rect.x = W-t_rect.width

    collideBalls()

    sc.blit(bg, (0, 0))
    balls.draw(sc)
    sc.blit(score, (0, 0))
    sc_text = f.render(str(game_score), 1, (94, 138, 14))
    sc.blit(sc_text, (20, 10))
    sc.blit(telega, t_rect)

    pygame.display.update()
    clock.tick(FPS)
    balls.update(H)

'''
pygame.mixer.music.load('V.flac')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play()

TRUE = 1
UNTRUE = 0
W = 600
H = 400

sc = pygame.display.set_mode((W, H))
pygame.display.set_caption("Класс Surface")
pygame.display.set_icon(pygame.image.load("rur.png"))

car_surf = pygame.image.load("images/car.bmp").convert()
car_rect = car_surf.get_rect(center=(W//2, H//2))
bg_surf = pygame.image.load("images/sand.jpg").convert()
car_surf.set_colorkey((255, 255, 255))
finish_surf = pygame.image.load("images/finish.png").convert_alpha()
bg_surf = pygame.transform.scale(
    bg_surf, (bg_surf.get_width()//3, bg_surf.get_height()//3))

WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
YELLOW = (239, 228, 176)

FPS = 60        # число кадров в секунду
clock = pygame.time.Clock()

car_up = car_surf
car_down = pygame.transform.flip(car_surf, 0, 1)
car_left = pygame.transform.rotate(car_surf, 90)
car_right = pygame.transform.rotate(car_surf, -90)

car = car_up
speed = 5

while TRUE:

    keys = pygame.key.get_pressed()
    mouses = pygame.mouse.get_pressed()[0]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            TRUE = UNTRUE
        if keys[pygame.K_ESCAPE]:
            TRUE = UNTRUE
        if keys[pygame.K_LEFT]:
            car = car_left
            car_rect.x -= speed
            if car_rect.x < 0:
                car_rect.x = 0
        elif keys[pygame.K_RIGHT]:
            car = car_right
            car_rect.x += speed
            if car_rect.x > W-car_rect.height:
                car_rect.x = W-car_rect.height
        elif keys[pygame.K_UP]:
            car = car_up
            car_rect.y -= speed
            if car_rect.y < 0:
                car_rect.y = 0
        elif keys[pygame.K_DOWN]:
            car = car_down
            car_rect.y += speed
            if car_rect.y > H-car_rect.height:
                car_rect.y = H-car_rect.height

    sc.blit(bg_surf, (0, 0))
    sc.blit(finish_surf, (0, 0))
    sc.blit(car, car_rect)

    clock.tick(FPS)
    pygame.display.update()
'''
'''
x, y = 10, 10
surf = pygame.Surface((x, y))
penny = 0
coor_x = W // 2
coor_y = H // 2

surf.fill(RED)

while TRUE:

    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            TRUE = UNTRUE
        if keys[pygame.K_ESCAPE]:
            TRUE = UNTRUE
        if keys[pygame.K_UP]:
            x += 1
            y += 1
            penny += 1
            if penny == 2:
                coor_x -= 1
                coor_y -= 1
                penny = 0
        if keys[pygame.K_DOWN]:
            x -= 1
            y -= 1
            penny += 1
            if penny == 2:
                coor_x += 1
                coor_y += 1
                penny = 0

    clock.tick(FPS)

    surf = pygame.Surface((x, y))
    surf.fill(RED)
    sc.fill(BLACK)
    sc.blit(surf, (coor_x, coor_y))
    pygame.display.update()
'''
'''
    surf = pygame.Surface((200, 200))
    surf.fill(RED)
    pygame.draw.circle(surf, GREEN, (100, 100), 80)
    # surf.set_alpha(128)

    surf_alpha = pygame.Surface((W, 200))
    pygame.draw.rect(surf_alpha, BLUE, (0, 0, W, 100))
    surf.set_alpha(128)

    surf_alpha.blit(surf, (0, 0))
    sc.blit(surf_alpha, (50, 50))
    pygame.display.update()
'''
pygame.quit()
sys.exit()
'''
import pygame as pg
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

cubeVertices = ((1, 1, 1), (1, 1, -1), (1, -1, -1), (1, -1, 1),
                (-1, 1, 1), (-1, -1, -1), (-1, -1, 1), (-1, 1, -1))
cubeEdges = ((0, 1), (0, 3), (0, 4), (1, 2), (1, 7), (2, 5),
             (2, 3), (3, 6), (4, 6), (4, 7), (5, 6), (5, 7))
cubeQuads = ((0, 3, 6, 4), (2, 5, 6, 3), (1, 2, 5, 7),
             (1, 0, 4, 7), (7, 4, 6, 5), (2, 3, 0, 1))


def wireCube():
    glBegin(GL_LINES)
    for cubeEdge in cubeEdges:
        for cubeVertex in cubeEdge:
            glVertex3fv(cubeVertices[cubeVertex])
    glEnd()


def solidCube():
    glBegin(GL_QUADS)
    for cubeQuad in cubeQuads:
        for cubeVertex in cubeQuad:
            glVertex3fv(cubeVertices[cubeVertex])
    glEnd()


def main():
    pg.init()
    display = (1680, 1050)
    pg.display.set_mode(display, DOUBLEBUF | OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0, 0.0, -5)

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                quit()

        glRotatef(1, 1, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        solidCube()
        # wireCube()
        pg.display.flip()
        pg.time.wait(10)


if __name__ == "__main__":
    main()
'''
